﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GN.Pay.TradeChannel;
using GN.Pay.TradeChannel.AliPay;
using GN.Pay.Utils;
using GN.Pay.Application.GatewayApp;

namespace GN.Pay.GatewayWeb.Controllers.Notify
{
    /// <summary>
    /// 支付宝通知
    /// </summary>
    public class AliPayController : APayNotifyController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tradeNotifyAppService"></param>
        public AliPayController(ITradeNotifyAppService tradeNotifyAppService)
            : base(tradeNotifyAppService)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        protected override string ChannelId
        {
            get
            {
                return "AliPay.Transfer";
            }
        }

        protected override NotifyMessage GetPayNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = AliPayServiceBase.GetPayOrderNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        protected override NotifyMessage GetRefundNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = AliPayServiceBase.GetRefundNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OrderPay()
        {
            DictionaryPayNotify();
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OrderRefund()
        {
            DictionaryRefundNotify();
        }

        private ActionResult OrderPayReturn(Func<ActionResult> errorActionResult, Func<string, ActionResult> actionResult)
        {
            try
            {
                var msg = GetPayNotifyMessage(Request);
                ITradeChannel channel = GetPayTradeChannel(msg);
                if (channel == null)
                {
                    Logger.Error("付款通知错误,无 " + ChannelId + " 实现");
                    return errorActionResult();
                }
                AliPayServiceBase aliPay = channel as AliPayServiceBase;
                if (aliPay == null)
                {
                    Logger.Error(channel.ChannelId + ":未能转换对应的支付类型");
                    return errorActionResult();
                }
                var entity = aliPay.OrderPayNotifyResponse((IDictionary<string, string>)msg.Message);
                if (!entity.IsSuccess())
                {
                    return actionResult(msg.UniqueNo);
                   // return errorActionResult();
                }
                return actionResult(entity.Out_Trade_No);
            }
            catch (Exception err)
            {
                Logger.Error(ChannelId + "出错", err);
                return errorActionResult();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ActionResult OrderPayReturn()
        {
            return OrderPayReturn(ErrorView, ReturnWaitResult);
        }

        public override ActionResult OrderPayWapReturn()
        {
            return OrderPayReturn(WapErrorView, ReturnWapWaitResult);
        }
    }
}